//
//  ChatTableViewCell.m
//  EaseMobDemo
//
//  Created by yutao on 16/2/27.
//  Copyright © 2016年 yutao. All rights reserved.
//

#import "ChatTableViewCell.h"

#define LABELWIDTH  160

@implementation ChatTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        [self createSubviews];
    }

    return self;
}

-(void)createSubviews
{
    self.contentView.backgroundColor = [UIColor greenColor];

    self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 60, 80)];
    self.bgImageView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.bgImageView];

    
    self.contentsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, LABELWIDTH, 160)];
    [self.contentView addSubview:self.contentsLabel];
    //设置无限行数
    self.contentsLabel.numberOfLines = 0;
    //换行模式:按文字换行
    self.contentsLabel.lineBreakMode = NSLineBreakByWordWrapping;


}
 

-(void)setMessage:(EMMessage *)message
{
    _message = message;

        id body = [message.messageBodies lastObject];

        EMTextMessageBody *textBody = body;
        self.contentsLabel.text = textBody.text;

        //label高度自适应
        CGFloat height = [[self class] heightForCell:textBody.text];
        CGRect tempRect = self.contentsLabel.frame;
        tempRect.size.height = height;
        self.contentsLabel.frame = tempRect;
        [self.contentsLabel sizeToFit];


//        图片自适应
        UIImage *image = [UIImage imageNamed:@"chat_receiver_bg"];

        image = [image resizableImageWithCapInsets:(UIEdgeInsetsMake(image.size.height * 0.6, image.size.width * 0.4, image.size.height * 0.3, image.size.width * 0.4))];
        self.bgImageView.image = image;

        //图片添加约束
        [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.edges.equalTo(self.contentsLabel).mas_equalTo(UIEdgeInsetsMake(-5 ,-15, -5, -15));


        }];    
}

-(void)setContents:(NSString *)contents
{

        self.contentsLabel.text = contents;

        //label高度自适应
        CGFloat height = [[self class] heightForCell:contents];
        CGRect tempRect = self.contentsLabel.frame;
        tempRect.size.height = height;
        self.contentsLabel.frame = tempRect;
        [self.contentsLabel sizeToFit];


        //        图片自适应
        UIImage *image = [UIImage imageNamed:@"chat_receiver_bg"];
        image = [image resizableImageWithCapInsets:(UIEdgeInsetsMake(image.size.height * 0.6, image.size.width * 0.4, image.size.height * 0.3, image.size.width * 0.4))];
        self.bgImageView.image = image;

        //图片添加约束
        [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.edges.equalTo(self.contentsLabel).mas_equalTo(UIEdgeInsetsMake(-5 ,-15, -5, -15));
            
            
        }];

}

+(CGFloat)heightForCell:(NSString *)content
{
    //根据内容算内容高度
    //参数1.指定内容宽度来计算内容高度
    //参数2.指定计算高度的方式
    //参数3.指定内容文字大小来计算内容高度
    //参数4.苹果备用参数,设置为nil
    CGSize size = CGSizeMake(LABELWIDTH, 10000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];

    CGRect rect = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

    return rect.size.height;
    
}



//自己发的
-(void)changeLabelLayout
{

    [self.contentsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(375-LABELWIDTH);
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(-5);
        make.right.mas_equalTo(-30);
//        make.width.mas_equalTo(150);
    }];
    
    self.contentsLabel.textAlignment = NSTextAlignmentRight;

    //添加约束
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {

//        make.edges.equalTo(self.contentsLabel).mas_equalTo(UIEdgeInsetsMake(-5, 0, 0, -280));
        make.left.mas_equalTo(self.contentsLabel.mas_left).offset(-5);
        make.bottom.mas_equalTo(self.contentsLabel.mas_bottom).offset(0);
        make.top.mas_equalTo(self.contentsLabel.mas_top).offset(10);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-10);

    }];


    UIImage *image = [UIImage imageNamed:@"chat_to"];
    image = [image resizableImageWithCapInsets:(UIEdgeInsetsMake(image.size.height * 0.6, image.size.width * 0.4, image.size.height * 0.3, image.size.width * 0.4))];
    self.bgImageView.image = image;
    
}









- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
